package teacher.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import database.dao.MysqlCrud;

import teacher.bean.Paper;
import teacher.bean.Teacher;
import teacher.bean.Patent;
import teacher.bean.Treatise;
import teacher.page.Page;
import tools.StringDate;

public class patent {
	
	/**
	 * @category 通过论著ID查询一项论著
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public Patent selectPatentById(int a) {
		Patent p = new Patent();
		String sql= "select * from patent where patId=?";
		int i=0;	
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(a)};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		if(1==list.size()){
			Map map=list.get(0);
			p=new MapToBean().MapToPatent(map);
		
		}
		return p;
	}
	
	/**
	 * @category 用于专利信息的修改
	 * @param treatise 修改的内容
	 * @param a 定位修改的位置
	 * @return 修改成功，返回true，否则返回flase
	 */
	public boolean updatePatent(Patent patent,int a) {
		String sql= "update patent set patId=?,name=?,fiestInventor=?,empowerDate=?,empowerUnit=?,transferFunds=?,examine=?,applyNumber=?,number=?,upload=?,teaId=?,expClass=?,empowerNum=?,applyDate=? where patId="+a+"";
		String[] parameter = {
				String.valueOf(patent.getPatId()),
				String.valueOf(patent.getName()),
				String.valueOf(patent.getFiestInventor()),
				String.valueOf(patent.getEmpowerDate()),
				String.valueOf(patent.getEmpowerUnit()),
				String.valueOf(patent.getTransferFunds()),
				String.valueOf(patent.getExamine()),
				String.valueOf(patent.getApplyNumber()),
				String.valueOf(patent.getNumber()),
				String.valueOf(patent.getUpload()),
				String.valueOf(patent.getTeaId()),
				String.valueOf(patent.getExpClass()),
				String.valueOf(patent.getEmpowerNum()),
				String.valueOf(patent.getApplyDate())
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 统计一个老师一定时间专利的数量
	 * @return sum int 论著的数量
	 */
		public int getCountPatentTeaTime(Teacher tea,Date date1,Date date2)
		{
			int sum=0;
			String[] parameter={String.valueOf(tea.getTeaId())};
			String sql="select count(*) from patent where empowerDate>='"+date1+"' and empowerDate<='"+date2+"' and teaId=?";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists=mysqlcurd.SelectData(sql, parameter);
			Map map=lists.get(0);
			sum=Integer.parseInt(map.get(0).toString());
			return sum;
		}
		
		/**
		 * @category 统计一个老师按照模糊查询专利的数量
		 * @return sum int 论著的数量
		 */
			public int getCountPatentTeaVague(String vague,Teacher t)
			{
				int sum=0;
				String[] parameter={String.valueOf(t.getTeaId())};
				String sql="select count(*) from patent where name like '%"+vague+"%' and teaId=?";
				MysqlCrud mysqlcurd=new MysqlCrud();
				List<Map> lists=mysqlcurd.SelectData(sql, parameter);
				Map map=lists.get(0);
				sum=Integer.parseInt(map.get(0).toString());
				return sum;
			}
	
	/**
	 * @category 通过老师查询专利分页
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Patent> selectPatentByTeacherPage(Teacher t1,Page page) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where acheve.teaId=? limit ?,?";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={t1.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 通过时间段查询老师专利分页
	 * @return paperOne List<Treatise> 返回查询到的专利列表
	 */
	public List<Patent> selectTimePatentTeaPage(Teacher t,Date date1,Date date2,Page page) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where acheve.empowerDate>='"+date1+"' and acheve.empowerDate<='"+date2+"' and acheve.teaId=? limit ?,?";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={t.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 通过字段模糊查询老师专利分页
	 * @param vague 模糊查询需要字段
	 * @return treatiseOne List<Treatise> 返回查询到的论著列表
	 */
	public List<Patent> selectVaguePatentTeaPage(String vague,Teacher t1,Page page) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where acheve.name like '%"+vague+"%' and acheve.teaId=? limit ?,?";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={ t1.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 通过老师查询专利
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Patent> 返回查询到的论文列表
	 */
	public List<Patent> selectPatentByTeacher(Teacher tea) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as t where acheve.teaId = t.teaId and t.user = ?";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 查询所有专利
	 * @return paperOne List<Patent> 返回查询到的论文列表
	 */
	public List<Patent> selectAllPatent() {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where (acheve.examine=4 or acheve.examine=5)";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, null);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 通过老师查询审核状态为已报省厅专利
	 * @param tea Teacher 老师信息
	 * @return patentOne List<Patent> 返回查询到的论文列表
	 */
	public List<Patent> selectYesPass(Teacher tea) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as t where acheve.teaId = t.teaId and acheve.examine=1; and t.user = ?";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 通过老师查询审核状态为未审核专利
	 * @param tea Teacher 老师信息
	 * @return patentOne List<Patent> 返回查询到的论文列表
	 */
	public List<Patent> selectNoPass(Teacher tea) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as t where acheve.teaId = t.teaId and acheve.examine=0; and t.user = ?";
		int i=0;
		List<Patent> patentOne=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			patentOne.add(new MapToBean().MapToPatent(map));
			i++;
		}
		return patentOne;
	}
	
	/**
	 * @category 统计专利的数量
	 * @return sum int 专利的数量
	 */
		public int getCountPatent()
		{
			int sum=0;
			String sql="select count(*) from patent where examine>3";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists=mysqlcurd.SelectData(sql, null);
			Map map=lists.get(0);
			sum=Integer.parseInt(map.get(0).toString());
			return sum;
		}
	
		/**
		 * @category 通过字段模糊查询专利
		 * @param vague 模糊查询需要字段
		 * @return patentOne List<Patent> 返回查询到的专利列表
		 */
		public List<Patent> selectVaguePatent(String vague) {
			String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where (acheve.examine=4 or acheve.examine=5) and acheve.name like '%"+vague+"%'";
			int i=0;
			List<Patent> patentOne=new ArrayList<Patent>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> list = mysqlcurd.SelectData(sql, null);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				patentOne.add(new MapToBean().MapToPatent(map));
				i++;
			}
			return patentOne;
		}
		
		/**
		 * @category 通过字段模糊查询老师专利
		 * @param vague 模糊查询需要字段
		 * @return patentOne List<Patent> 返回查询到的专利列表
		 */
		public List<Patent> selectVaguePatentTea(String vague,Teacher t) {
			String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where acheve.name like '%"+vague+"%' and acheve.teaId=?";
			int i=0;
			List<Patent> patentOne=new ArrayList<Patent>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			String[] parameter={ String.valueOf(t.getTeaId())};
			List<Map> list = mysqlcurd.SelectData(sql, parameter);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				patentOne.add(new MapToBean().MapToPatent(map));
				i++;
			}
			return patentOne;
		}
		
		/**
		 * @category 通过时间段查询专利
		 * @return paperOne List<Paper> 返回查询到的专利列表
		 */
		public List<Patent> selectTimePatent(Date date1,Date date2) {
			String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where (acheve.examine=4 or acheve.examine=5) and acheve.empowerDate>='"+date1+"' and acheve.empowerDate<='"+date2+"'";
			int i=0;
			List<Patent> patentOne=new ArrayList<Patent>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> list = mysqlcurd.SelectData(sql, null);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				patentOne.add(new MapToBean().MapToPatent(map));
				i++;
			}
			return patentOne;
		}
		
		/**
		 * @category 通过时间段查询老师专利
		 * @return paperOne List<Paper> 返回查询到的专利列表
		 */
		public List<Patent> selectTimePatentTea(Teacher t,Date date1,Date date2) {
			String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve where acheve.empowerDate>='"+date1+"' and acheve.empowerDate<='"+date2+"' and acheve.teaId=?";
			int i=0;
			List<Patent> patentOne=new ArrayList<Patent>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			String[] parameter={ String.valueOf(t.getTeaId())};
			List<Map> list = mysqlcurd.SelectData(sql, parameter);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				patentOne.add(new MapToBean().MapToPatent(map));
				i++;
			}
			return patentOne;
		}

		/**
		 * @category 插入一条专利数据
		 * @param paper 所要插入的专利数据
		 * @return 插入成功返回true，否则返回false
		 */
		public boolean insertPatent(Patent patent){
			boolean flag = false;
			String sql="insert into patent(name,fiestInventor,empowerDate,empowerUnit,transferFunds,examine,applyNumber,number,upload,teaId,expClass,empowerNum,applyDate)"+
			           " values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			String[] parameter={
					String.valueOf(patent.getName()),
					String.valueOf(patent.getFiestInventor()),
					String.valueOf(patent.getEmpowerDate()),
					String.valueOf(patent.getEmpowerUnit()),
					String.valueOf(patent.getTransferFunds()),
					String.valueOf(patent.getExamine()),
					String.valueOf(patent.getApplyNumber()),
					String.valueOf(patent.getNumber()),
					String.valueOf(patent.getUpload()),
					String.valueOf(patent.getTeaId()),
					String.valueOf(patent.getExpClass()),
					String.valueOf(patent.getEmpowerNum()),
					String.valueOf(patent.getApplyDate())
			};
			MysqlCrud mysqlcurd=new MysqlCrud();
			flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
			return flag;
		}
		
		/**
		 * @category 统计一个老师专利的数量
		 * @return sum int 专利的数量
		 */
			public int getCountPatentTea(Teacher tea)
			{
				int sum=0;
				String[] parameter={String.valueOf(tea.getTeaId())};
				String sql="select count(*) from patent where teaId=?";
				MysqlCrud mysqlcurd=new MysqlCrud();
				List<Map> lists=mysqlcurd.SelectData(sql, parameter);
				Map map=lists.get(0);
				sum=Integer.parseInt(map.get(0).toString());
				return sum;
			}
			/**
			 * @category 用于删除专利的信息
			 * @return 删除成功返回true，否则为false
			 */
			public boolean DeletePatent(Patent patent){
				String sql = "delete from patent where patId=?";
				String[]  parameter={String.valueOf(patent.getPatId())};
				 MysqlCrud mysqlcurd=new MysqlCrud();
				 boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
				 return flag;
			}
}
